<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use app\portal\model\PortalCategoryModel;
use app\admin\model\RouteModel;

class ListController extends HomeBaseController
{
    public function index()
    {
        $id = $this->request->param('id', 0, 'intval');
        $portalCategoryModel = new PortalCategoryModel();

        $category = $portalCategoryModel->where('id', $id)->where('status', 1)->find();
        $categorys = $portalCategoryModel->where('parent_id', $id)->where('status', 1)->where("delete_time", "eq", 0)->select();

        $routeModel = new RouteModel();
        $newCategorys = array();
        foreach ($categorys as $ca) {
            $alias = $routeModel->getUrl('portal/List/all', ['id' => $ca['id']]);
            if (!empty($alias)) {
                $ca['href'] = "/".$alias.'.html';
            } else {
                $ca['href'] = "/portal/list/all/id/" . $ca['id'].'.html';
            }
            array_push($newCategorys, $ca);
        }

        $this->assign('category', $category);
        $this->assign('categorys', $newCategorys);

        $listTpl = empty($category['list_tpl']) ? 'list' : $category['list_tpl'];

        return $this->fetch('/' . $listTpl);
    }

    public function all()
    {
        $id = $this->request->param('id', 0, 'intval');
        $portalCategoryModel = new PortalCategoryModel();

        $category = $portalCategoryModel->where('id', $id)->where('status', 1)->find();
        $categorys = $portalCategoryModel->where('parent_id', $category['parent_id'])->where('status', 1)->where("delete_time", "eq", 0)->select();
        $cs = $portalCategoryModel->where('parent_id', $id)->where('status', 1)->select();

        $routeModel = new RouteModel();
        $newCategorys = array();
        foreach ($categorys as $ca) {
            $alias = $routeModel->getUrl('portal/List/all', ['id' => $ca['id']]);
            if (!empty($alias)) {
                $ca['href'] = "/".$alias.'.html';
            } else {
                $ca['href'] = "/portal/list/all/id/" . $ca['id'].'.html';
            }
            array_push($newCategorys, $ca);
        }

        $newCs = array();
        foreach ($cs as $c) {
            $alias = $routeModel->getUrl('portal/List/all', ['id' => $c['id']]);
            if (!empty($alias)) {
                $c['href'] = "/".$alias.'.html';
            } else {
                $c['href'] = "/portal/list/all/id/" . $c['id'].'.html';
            }
            array_push($newCs, $c);
        }

        $this->assign('category', $category);
        $this->assign('categorys', $newCategorys);
        $this->assign('cs', $newCs);


        $listTpl = empty($category['list_tpl']) ? 'list' : $category['list_tpl'];

        return $this->fetch('/' . $listTpl);
    }

    public function item()
    {
        $id = $this->request->param('id', 0, 'intval');
        $portalCategoryModel = new PortalCategoryModel();

        $category = $portalCategoryModel->where('id', $id)->where('status', 1)->find();
        $categorys = $portalCategoryModel->where('parent_id', $category['parent_id'])->where('status', 1)->where("delete_time", "eq", 0)->select();
        $cs = $portalCategoryModel->where('parent_id', $id)->where('status', 1)->select();
        $routeModel = new RouteModel();
        $newCategorys = array();
        foreach ($categorys as $ca) {
            $alias = $routeModel->getUrl('portal/List/all', ['id' => $ca['id']]);
            if (!empty($alias)) {
                $ca['href'] ="/".$alias.'.html';
            } else {
                $ca['href'] = "/portal/list/all/id/" . $ca['id'].'.html';
            }
            array_push($newCategorys, $ca);
        }

        $newCs = array();
        foreach ($cs as $c) {
            $alias = $routeModel->getUrl('portal/List/all', ['id' => $c['id']]);
            if (!empty($alias)) {
                $c['href'] = "/".$alias.'.html';
            } else {
                $c['href'] = "/portal/list/all/id/" . $c['id'].'.html';
            }
            array_push($newCs, $c);
        }

        $this->assign('category', $category);
        $this->assign('categorys', $newCategorys);
        $this->assign('cs', $newCs);

        $listTpl = empty($category['list_tpl']) ? 'list' : $category['list_tpl'];

        return $this->fetch('/' . $listTpl);
    }

}
